<template>
	<view class="index">
		<view class="logo">
			<view class="top">

				<image src="../../static/logo3.png" mode=""></image>
			</view>
			<view class="" style="text-align: center;font-size: 24px;">
				延代驾
			</view>

		</view>
		<!-- <view class="input active">
      <text class="icon">&#xe605;</text>
      <input type="number" value="" maxlength="11" id="mobile" v-model="info.mobile" placeholder="请输入手机号" />
    </view>

    <view class="input active">
      <text class="icon">&#xe60d;</text>
      <input type="password" value="" id="password" v-model="info.password" placeholder="请确认密码" />
    </view> -->

		<!-- <view class="link">
      <view class="left" @tap="forgetThePassword()">忘记密码？</view>
      <view class="right" @tap="register()">去注册</view>
    </view> -->


		<!-- <button type="primary" class="button" open-type="getUserInfo" @tap="login">立即登录</button> -->
		<!-- 		<button v-if="btnShow==true" style="background: #skyblue; color: aliceblue;" class="button"
			@click="Isshow()">微信授权登录</button>
		<button v-else style="background: #skyblue; color: aliceblue;" class="button" open-type="getPhoneNumber"
			@getphonenumber="getPhoneNumber">微信授权登录</button> -->

		<button style="background: #skyblue; color: aliceblue;" class="button" @click="login()">微信授权登录</button>
		<checkbox-group @change="checkboxChange" class="agreement_radio" style="margin-top: 3rem;">
			<checkbox :value='checkVal' :checked="checked" />
			<text class="text_left">已阅读并同意</text>

			<text class="text_right"> <text>《用户注册协议》</text> <text>《用户隐私协议》</text></text>

		</checkbox-group>
	</view>
</template>

<script>
	const app = getApp();
	import {
		setTokenStorage
	} from '../../utils/token';
	import logger from '../../utils/logger';
	import {
		genTestUserSig
	} from '../../debug/GenerateTestUserSig.js';
	const {
		getTokenStorage
	} = require('../../utils/token.js');

	import WXBizDataCrypt from '../../libs/WXBizDataCrypt.js'
	import helper from '../../common/helper.js';
	import api from '../../common/api.js';
	export default {
		data() {
			return {
				btnShow: true,
				mobile: "",
				iv: "",
				encryptedData: "",
				session_key: "",
				openid: "",
				openid: null,
				checkVal: '0',
				checked: false,
				nickname: "",
				focus: '',
				info: {},
				url: 'pages/index/index',
				redirectTo: false,
				obj: {},
				code: ''
			};
		},
		onLoad(option) {
			let that = this;
			let _this = this;

		},
		onShow() {
			console.log("onshow")
			this.getCode()
		},
		created() {
			uni.$on('isSDKReady', value => {
				console.log("创建ready")
				this.isSDKReady = value.isSDKReady;
			});
		},
		methods: {
			getCode() {
				wx.login({
					success(res) {
						if (res.code) {
							this.code = res.code;
						}
					}
				})
			},

			checkboxChange(e) {
				console.log(e.detail.value, '++++');
				if (e.detail.value == '0') {
					this.checked = true
					this.btnShow = false
				} else {
					this.checked = false

				}
			},


			getUserProfile() {
				let that = this
				uni.getUserProfile({
					desc: "用于完善用户信息",
					success: (res1) => {
						that.info = res1.userInfo;
						console.log("用户信息", res1)
						uni.showToast({
							icon: "none",
							title: '获取成功'
						})
					},
					fail: (err) => {
						console.log(err)
						uni.showToast({
							icon: "none",
							title: '用户拒绝获取'
						})
					}
				})
			},
			getPhoneNumber(e) {
				if (this.checked == false) {
					uni.showToast({
						icon: "error",
						title: "请阅读并同意 ",
						// content:"《用户注册协议》《用户隐私协议》"
					})
				} else {
					if (e.detail.errMsg == "getPhoneNumber:ok") {
						this.iv = e.detail.iv
						this.encryptedData = e.detail.encryptedData
						this.login()
					}
				}

			},



			toIndex() {
				uni.switchTab({
					url: this.url
				})
			},
			Isshow() {
				if (this.checked == true) {
					this.btnShow = false

				} else {
					this.btnShow = true
					uni.showToast({
						icon: "error",
						title: "请阅读并同意",
					})

				}
			},
			onInputFocus(e) {
				let id = e.target.id;
				this.focus = id;
			},
			async login() {
				if (this.checked == true) {
					this.btnShow = false

				} else {
					this.btnShow = true
					uni.showToast({
						icon: "error",
						title: "请阅读并同意",
					})
				}
				var _this = this;
				uni.login({
					provider: 'weixin',
					success: async loginRes => {
						//console.log("logincode",loginRes)
						// console.log("denglu" + loginRes.code)
						let code = _this.code == '' ? loginRes.code : _this.code;
						// 用code换取openid
						const ret = await api.getOpenid(code);
						if (ret.code == 0) {
							uni.hideLoading();
							uni.showModal({
								title: '操作提示',
								content: '获取openid失败，请检查相关配置',
								showCancel: false,
							});
							return false;
						}
						uni.setStorageSync("OPenid", ret.data.openid)
						this.info.openid = ret.data.openid;
						this.session_key = ret.data.session_key
						this.info.session_key = ret.data.session_key;
						this.obj.encryptedData = encodeURIComponent(this.encryptedData)
						this.obj.sessionKey = ret.data.session_key
						this.obj.iv = this.iv
						let reb = await api.getPhone(this.obj);
						/* if (reb == null) {
							// this.getPhoneNumber(e);
							uni.showModal({
								title: '操作提示',
								content: '授权登录失败',
								showCancel: false,
							});
							return false;
						} */
						this.info.mobile = '1'
						uni.setStorageSync("mobile", "0")
						const SDKAppID = app.globalData.SDKAppID;
						const userID = this.info.mobile.toString();
						const userSig = genTestUserSig(userID).userSig;
						const config = {
							userID: userID,
							userSig: userSig
						}
						uni.setStorageSync('config', config)
						let res = await api.login(this.info);
						const userid = res.data.userinfo.id
						uni.setStorageSync('userid', userid)
						uni.hideLoading();
						helper.toast(res.msg);
						if (res.code == 1) {
							helper.setUserToken(res.data.userinfo.token);
							setTokenStorage({
								userInfo: app.globalData.userInfo
							});
							wx.setStorageSync(`TIM_${getApp().SDKAppID}_isTUIKit`, true);
							uni.$TUIKit.login({
								userID: userID,
								userSig: userSig
							}).then(() => {
								uni.$aegis.reportEvent({
									name: 'login',
									ext1: 'login-success',
									ext2: 'uniTuikitExternal',
									ext3: `${SDKAppID}`,
								})
							}).catch((error) => {
								uni.$aegis.reportEvent({
									name: 'login',
									ext1: `login-failed#error:${error}`,
									ext2: 'uniTuikitExternal',
									ext3: `${SDKAppID}`,
								})
							})
							uni.reLaunch({
								url: "../mine/mine",
							});
						}
					}
				});
			},

			register() {
				uni.reLaunch({
					url: 'register'
				});
			},
			forgetThePassword() {
				uni.navigateTo({
					url: 'forgetThePassword'
				});
			}
		},
		onLaunch() {
			this.getUserProfile()

		}


	};
</script>

<style lang="scss">
	.icon {
		color: #acacac;
	}

	.index {
		padding: 0 10%;
	}

	.logo {
		margin-top: 10%;
		margin-bottom: 10%;

		.top {
			display: flex;
			align-items: flex-end;
			font-size: 80upx;
			// margin-bottom: 20upx;
			justify-content: center;

			image {
				width: 200upx;
				height: 258upx;
			}
		}

		.bottom {
			font-weight: 800;
			letter-spacing: 10upx;
		}
	}

	.input {
		display: flex;
		align-items: center;
		border-bottom: 1px #f8f8f8 solid;
		padding: 26upx 0;

		text {
			width: 10%;
		}

		input {
			width: 90%;
		}
	}

	.yzm {
		input {
			width: 60%;
		}

		.right {
			width: 30%;
			text-align: right;
			font-size: 28upx;
		}
	}

	.active {
		border-bottom: #4cd964 1px solid;
	}

	.link {
		margin-top: 40upx;
		font-size: 26upx;
		width: 100%;
		display: flex;
		justify-content: space-between;
	}

	.button {
		height: 44pt;
		line-height: 44pt;
		text-align: center;
		width: 100%;
		border-radius: 50upx;
		margin-top: 10%;
		font-size: 30upx;
		background-color: #3294FF !important;
		border: none;
		box-shadow: 0px 5px 10px 1px #c1d6f2;
	}

	.to-index {
		text-align: center;
		margin-top: 20upx;
	}

	.text_right {
		color: #3294FF;
	}
</style>